/**
 * Superclass for all controls based on input HTML-element.
 *
 * @author Andrey Anisimov <hercules@inbox.ru>
 * @see core.ui.FocusWindow
 */

import core.ui.FocusWindow;

class InputWindow extends FocusWindow {
	
	/**
	 * Sets whether the input is selected.
	 *
	 * @param selected (bool) true to select, false to unselect.
	 */
	this.setSelected = function(selected) {
		if (selected) {
			this.getInput().select();
		} else {
			this.getInput().blur();
		}
	}

	/**
	 * Sets the input name.
	 * Will NOT work in IE 5-6 since IE doesn't
	 * support name changes after input creation.
	 * 
	 * @param name (string) input name
	 */
	this.setName = function(name) {
		this.getInput().name = name;
	}
	
	/**
	 * Returns input field name.
	 *
	 * @return string
	 */
	this.getName = function() {
		return this.getInput().name;
	}
	
	/**
	 * Sets input value.
	 *
	 * @param value (string) new value
	 */
	this.setValue = function(value) {
		this.getInput().value = value;
	}
	
	/**
	 * Returns current input value.
	 *
	 * @return string
	 */
	this.getValue = function() {
		return this.getInput().value;
	}
	
	/**
	 * Sets input text.
	 *
	 * @param text (string) new text
	 * @see setValue
	 */
	this.setText = function(text) {
		this.setValue(text);
	}
	
	/**
	 * Returns current input text.
	 *
	 * @return string
	 * @see getValue
	 */
	this.getText = function() {
		return this.getValue();
	}

	/**
	 * Sets whether the input has focus.
	 *
	 * @param focus (bool) true if focused, false otherwise
	 */
	this.setFocus = function(focus) {
		if (focus) {
			try {
				this.getInput().focus();
			} catch (ex) {
				// Can't move focus to the control because it's invisible or disabled.
			}
		} else {
			this.getInput().blur();
		}
	}
	
	/**
	 * Sets the tab index for input.
	 *
	 * @param index (int) tab index
	 */
	this.setTabIndex = function(index) {
		this.getInput().tabIndex = index;
	}
	
	/**
	 * Returns current tab index.
	 *
	 * @return int
	 */
	this.getTabIndex = function() {
		return getInput().tabIndex;
	}
	
	/**
	 * Sets the access key for input field.
	 *
	 * @param key (int) new access key
	 */
	this.setAccessKey = function(key) {
		this.getInput().accessKey = key;
	}
	
	/**
	 * Return current access key.
	 *
	 * @return int
	 */
	this.getAccessKey = function() {
		return this.getInput().accessKey;
	}

	/**
	 * Returns input element.
	 *
	 * @return HTMLInputElement
	 * @see getElement
	 */
	this.getInput = function() {
		return this.getElement();
	}
	
	/**
	 * @protected
	 * Creates input element.
	 *
	 * @return HTMLInputElement
	 */
	this.createElement = function() {
		var input = document.createElement('input');
		input.type = this.getType();
		return input;
	}
	
	/**
	 * @protected
	 * Returns input control type (text by default).
	 * 
	 * @return string
	 */
	this.getType = function() {
		return 'text';
	}
}